package ink.lovejinhu.leetcode;

import ink.lovejinhu.common.TreeNode;

public class Solution_0653 {

    public boolean findTarget(TreeNode root, int k) {
        return this.dfs(root, k, 2);
    }
    public boolean dfs(TreeNode root, int k, int count) {
        if (root == null) {
            return false;
        }

        if (root.val == k) {
            if (count == 1) {
                return true;
            } else {
                return dfs(root.left, 0, 1);
            }
        }
        if (root.val > k) {
            return dfs(root.left, k, count);
        } else {
            if (k - root.val > root.val) {
                if (count - 1 <= 0) {
                    return false;
                }
                return dfs(root.right, k - root.val, count - 1);
            } else {
                if (count - 1 <= 0) {
                    return false;
                }
                return dfs(root.left, k, count - 1);

            }
        }
    }
}
